package com.study.langchain4j.config;

import com.study.langchain4j.service.BaseHighLevelHelloWorldChatService;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author xl-9527
 * @since 2025/10/26
 **/
@Configuration(proxyBeanMethods = false)
public class Langchain4jConfig {

    public final static String BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";

    @Bean
    ChatModel chatModel(@Value("${dashscope.api-key}") final String apiKey, final List<ChatModelListener> listeners) {
        return new OpenAiChatModel.OpenAiChatModelBuilder()
                .apiKey(apiKey)
                .baseUrl(BASE_URL)
                .logRequests(true)
                .logResponses(true)
                .modelName("qwen3-max")
                .listeners(listeners)
                .build();
    }

    @Bean
    ChatModel chatModelDS(@Value("${dashscope.api-key}") final String apiKey) {
        return new OpenAiChatModel.OpenAiChatModelBuilder()
                .apiKey(apiKey)
                .baseUrl(BASE_URL)
                .logRequests(true)
                .logResponses(true)
                .modelName("deepseek-v3.1")
                .build();
    }

    /* 多模态的模型，他具有视觉理解能力 */
    @Bean
    ChatModel visionModel(@Value("${dashscope.api-key}") final String apiKey) {
        return new OpenAiChatModel.OpenAiChatModelBuilder()
                .apiKey(apiKey)
                .baseUrl(BASE_URL)
                .logRequests(true)
                .modelName("qwen3-vl-plus")
                .build();
    }

    /* 高级 API 的使用 */
    @Bean
    BaseHighLevelHelloWorldChatService createBean(@Qualifier("chatModel") final ChatModel chatModel) {
        return AiServices.create(BaseHighLevelHelloWorldChatService.class, chatModel);
    }
}
